Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #1588

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

VasylPylypchynets
Copy link

Copy link

@TarasHoliuk TarasHoliuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have too much states - try to remove part of them and simplify logic

Feels free to ask for help in the chat if you need 🙂

src/api/todos.ts Outdated
return client.get<Todo[]>(`/todos?userId=${USER_ID}`);
};

// Add more methods here

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can delete this comment now :)

src/App.tsx Outdated
}

function handleUpdateAllTodos() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the last todo has loader when I click toggle all:
image

src/App.tsx Outdated
return null;
});

const updatedTodo = await Promise.all(updatedTodos);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const updatedTodo = await Promise.all(updatedTodos);
const updatedTodos = await Promise.all(updatedTodos);

But Promise.all will be rejected if at least one todo update was failed. You should handle result for each todo separately

src/App.tsx Outdated
const [itemEditingId, setItemEditingId] = useState<number | null>(null);
const [newTitle, setNewTitle] = useState<string>('');
const [todosLength, setTodosLength] = useState<number>(0);
const [allTodos, setAllTodos] = useState<Todo[]>([]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need todos and allTodos states. Keep only one (name todos, but store all todos there) state and compute filtered todos based on other states and save filtered array into a variable

Also do filtering inside useMemo callback

src/App.tsx Outdated
return <UserWarning />;
const [query, setQuery] = useState<string>('');
const [todos, setTodos] = useState<Todo[]>([]);
const [itemsLeft, setItemsLeft] = useState<number>(0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General rule - do not create state if you can compute it based on other state or props values

src/App.tsx Outdated
}
}

function handleDeleteTodo(id: number) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

such functions are redundant - just use setDeleteTodoId everywhere instead of handleDeleteTodo

src/App.tsx Outdated
}

function handleCleanCompleted() {
setCleanCompleted(true);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this state seems redundant
Do something directly inside handler instead of changing state and do it in useEffect callback

src/App.tsx Outdated
const [isSubmiting, setIsSubmiting] = useState<boolean>(false);
const [isUpdating, setIsUpdating] = useState<number | null>(null);
const [itemEditingId, setItemEditingId] = useState<number | null>(null);
const [newTitle, setNewTitle] = useState<string>('');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need query, newTask and newTitle here?

For editing move state (and part of related logic, for example itemEditingId state - you can make it just boolean inside of that component) into TodoItem component

For input in header you need only one state

Active
</a>

<a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each link is almost the same - try to get rid of code duplication

inputRef.current.focus();
onSetNewTitle(todo.title);
}
}, [itemEditingId, todo.id]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix lint:
image

Copy link

@TarasHoliuk TarasHoliuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

Vasyl Pylypchynets added 2 commits January 7, 2025 22:22
Copy link

@volodymyr-soltys97 volodymyr-soltys97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GJ!

Comment on lines 34 to 37
onClick={e => {
e.preventDefault();
onSortBy(filter);
}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onClick={e => {
e.preventDefault();
onSortBy(filter);
}}
onClick={() => onSortBy(filter)}

Vasyl Pylypchynets added 2 commits January 9, 2025 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants